package com.rpframework.module.common.bottom.entity.enums;

import com.rpframework.module.common.springmvc.annotation.AutoMapping;
import com.rpframework.utils.URLUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 请求链接类型
 */
public enum RequestType {
    API("接口", "/api/json"), PAGE("页面", "/page"), WX_CALLBACK("微信回调", "/wx/callback"), NONE("没有类型", "");
    public static RequestMappingHandlerMapping requestMappingHandler = null;

    public static String controllerFix;
    /**
     * 说明
     */
    private String explain;
    /**
     * URL前缀
     */
    private String uriFix;

    public String getUriFix() {
        return uriFix;
    }

    RequestType(String explain, String uriFix) {
        this.explain = explain;
        this.uriFix = uriFix;
    }

    public String getExplain() {
        return explain;
    }

    public void setExplain(String explain) {
        this.explain = explain;
    }


    /**
     * 转换URL
     *
     * @param clazz 类
     * @return uei
     */
    public static String[] parseUri(Class<?> clazz) {
        RequestMapping requestMappingAnnotation = clazz.getAnnotation(RequestMapping.class);
        if (requestMappingAnnotation == null) {
            return new String[]{};
        }
        String[] patterns = parseUri(requestMappingAnnotation);
        if (patterns.length != 0) {
            return patterns;
        }
        ResponseBody annotation = clazz.getAnnotation(ResponseBody.class);
        AutoMapping autoMapping = clazz.getAnnotation(AutoMapping.class);
        if (autoMapping != null) {
            if (!autoMapping.auto()) {
                return new String[]{autoMapping.value().getUriFix()};
            } else {
                return new String[]{parseUri(clazz, autoMapping.value().getUriFix())};
            }
        } else {
            if (annotation != null) {
                return new String[]{parseUri(clazz, RequestType.API.getUriFix())};
            } else {
                return new String[]{parseUri(clazz, RequestType.PAGE.getUriFix())};
            }
        }
    }

    /**
     * 转换URL
     *
     * @return uei
     */
    public static String[] parseUri(Method method) {
        String[] patterns = parseUri(method.getAnnotation(RequestMapping.class));
        if (patterns != null && (patterns.length == 0)) {
            patterns = new String[]{URLUtils.humpParseUri(method.getName())};
        }
        return patterns;
    }

    public static String[] parseUri(RequestMapping annotation) {
        final Class<RequestMappingHandlerMapping> clazz = RequestMappingHandlerMapping.class;
        String[] patterns;
        try {
            Method declaredMethod =
                    clazz.getDeclaredMethod("resolveEmbeddedValuesInPatterns", String[].class);
            declaredMethod.setAccessible(true);
            patterns = (String[]) declaredMethod.invoke(requestMappingHandler, (Object) annotation.value());
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        return patterns;
    }

    /**
     * 转换URL
     *
     * @param clazz clazz 类
     * @param fix   前缀
     * @return uri
     */
    public static String parseUri(Class<?> clazz, String fix) {
        String simpleName = clazz.getSimpleName();
        if (!StringUtils.isBlank(controllerFix)) {
            int controller = simpleName.indexOf(controllerFix);
            if (controller > 0) {
                simpleName = simpleName.substring(0, controller);
            }
        }
        return fix + URLUtils.humpParseUri(simpleName);
    }
}
